#include "hsystemtrayicon.h"

HSystemTrayIcon::HSystemTrayIcon()
{
    this->createTrayMenu();
}

void HSystemTrayIcon::onOpenMonitorClick(){
    emit onMenuItemClick(MenuItem::Monitor);
}

void HSystemTrayIcon::onAboutAppClick(){
    emit onMenuItemClick(MenuItem::About);
}

void HSystemTrayIcon::onExitAppClick(){
    emit onMenuItemClick(MenuItem::Exit);
}

void HSystemTrayIcon::createTrayMenu(){
    this->setIcon(QIcon(":/icon.png"));
    this->setToolTip(tr("流量监控悬浮框"));
    QMenu *menu = new QMenu();
    QString menuQss = readTrayMenuStyleConfig();
    if(menuQss != NULL)
        menu->setStyleSheet(menuQss);
    //日历天气
    QWidgetAction *calendarAction = new QWidgetAction(this);
    QLabel *lbDateTime = new QLabel();
    lbDateTime->setObjectName("lbDateTime");
    lbDateTime->setAlignment(Qt::AlignmentFlag::AlignVCenter|Qt::AlignmentFlag::AlignHCenter);
    lbDateTime->setMinimumHeight(35);
    lbDateTime->setMaximumHeight(35);
    lbDateTime->setBackgroundRole(QPalette::ColorRole::Light);
    lbDateTime->setText(QDateTime(QDateTime::currentDateTime()).toString(Qt::DateFormat::LocalDate));
    calendarAction->setDefaultWidget(lbDateTime);
    menu->addAction(calendarAction);
    menu->addSeparator();
    //任务管理器
    QWidgetAction *monitorAction = new QWidgetAction(this);
    QPushButton *monitor = new QPushButton();
    monitor->setObjectName("btnMonitor");
    monitor->setText(tr("任务管理器"));
    monitor->setFlat(true);
    QObject::connect(monitor, SIGNAL(clicked()), this, SLOT(onOpenMonitorClick()));
    monitorAction->setDefaultWidget(monitor);
    menu->addAction(monitorAction);
    menu->addSeparator();
    //退出或者关于
    QWidgetAction *exitWidgetAction =  new QWidgetAction(this);
    QHBoxLayout *exitContainer = new QHBoxLayout();
    exitContainer->setSpacing(0);
    exitContainer->setContentsMargins(0,0,0,0);
    exitContainer->setMargin(0);
    exitContainer->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    exitContainer->setDirection(QBoxLayout::Direction::LeftToRight);
    QPushButton *about = new QPushButton();
    about->setObjectName("btnAbout");
    about->setText(tr("关于"));
    about->setIcon(QIcon(":/assets/images/menu/about.png"));
    about->setFlat(true);
    QObject::connect(about, SIGNAL(clicked()), this, SLOT(onAboutAppClick()));
    exitContainer->addWidget(about, 1);
    QPushButton *exit = new QPushButton();
    exit->setObjectName("btnExit");
    exit->setText(tr("退出"));
    exit->setIcon(QIcon(":/assets/images/menu/exit.png"));
    exit->setFlat(true);
    QObject::connect(exit, SIGNAL(clicked()), this, SLOT(onExitAppClick()));
    exitContainer->addWidget(exit, 1);
    QWidget *exitWidget = new QWidget();
    exitWidget->setLayout(exitContainer);
    exitWidgetAction->setDefaultWidget(exitWidget);
    menu->addAction(exitWidgetAction);
    this->setContextMenu(menu);
}

QString HSystemTrayIcon::readTrayMenuStyleConfig(){
    QFile *qss = new QFile(":/assets/styles/system-tray-menu.css");
    if(qss->open(QIODevice::ReadOnly)){
        QString result = QString(qss->readAll());
        qss->close();
        return result;
    }else{
        return NULL;
    }
}
